package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Authors;
import com.company.project.service.AuthorsService;
import com.company.project.service.impl.AuthorsServiceImpl;
import com.company.project.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;


/**
* Created by CodeGenerator on 2018/10/08.
*/

@RestController
@RequestMapping("/authors")
public class AuthorsController {
    @Resource
    @Autowired
    private AuthorsServiceImpl authorsServiceImpl;

     /**
     * 根据作者查询
     * @param
     * @return
     */
    @PostMapping(value = "/frontendQuery/{index}")
    public PageUtil frontendQuery(@RequestBody String cname,@PathVariable("index") Integer index)  {
        PageUtil pageUtil = authorsServiceImpl.frontendQuery(cname, index);
        return pageUtil;
    }
    //根据id查询稿件
    @RequestMapping(value = "/selectmanuscript/{id}",method = RequestMethod.GET)
    public List selectmanuscript(@PathVariable("id")Integer id){
        List selectmanuscript = authorsServiceImpl.selectmanuscript(id);
        return selectmanuscript;
    }
    //根据id查询评估
    @RequestMapping(value = "/selectAssessById/{id}",method = RequestMethod.GET)
    public List selectAssessById(@PathVariable("id")Integer id){
        List list = authorsServiceImpl.selectAssessById(id);
        return list;
    }
    @PostMapping("/add")
    public Result add(Authors authors) {
        authorsServiceImpl.save(authors);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        authorsServiceImpl.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Authors authors) {
        authorsServiceImpl.update(authors);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Authors authors = authorsServiceImpl.findById(id);
        return ResultGenerator.genSuccessResult(authors);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Authors> list = authorsServiceImpl.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}

